<?php
namespace app\common\controller;

/**
 * 学习类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/27
 * Time: 10:24
 */
class ClassLearn {

    /**
     * 处理用户超时的次数
     * @param $openid
     * @param $clid
     * @return int
     */
    public function deal_outtime_ci_by_user($openid,$clid){
        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['clid','=',$clid];
        $maps[] = ['end_time','=','0000-00-00 00:00:00'];
        $list = model('class_learn_ci')->get_list($maps);
        foreach($list as $k => $v){
            $r = $this->deal_outtime_ci($v);
        }
        return 1;
    }

    /**
     * 处理超时的次数 0000-00-00 00:00:00
     * @param $clc_detail
     * @return int
     */
    public function deal_outtime_ci($clc_detail){
        $now_time = date('Y-m-d H:i:s');
        if($now_time > $clc_detail['out_time'] && $clc_detail['end_time'] == '0000-00-00 00:00:00'){

            //处理分数
            $r = $this->deal_score_ci($clc_detail);

            $clc_data = [];
            $clc_data['end_time'] = $clc_detail['out_time'];

            $maps = [];
            $maps[] = ['id','=',$clc_detail['id']];
            $r = model('class_learn_ci')->where($maps)->update($clc_data);
        }
        return 1;
    }

    /**
     * 处理分数
     * @param $clc_detail
     * @return int
     */
    public function deal_score_ci($clc_detail){
        //处理答案，返回对与错的数量
        $answer_arr = $this->deal_answer($clc_detail);

        $maps = [];
        $maps[] = ['id','=',$clc_detail['clid']];
        $cl_detail = model('class_learn')->get_detail($maps,'score_type,per_ti_score,zeng_score');

        //判断打分类型：1，每题打分；2，全对打分
        $score = 0;
        if($cl_detail['score_type'] == 1){
            $score = $answer_arr['right_num'] * $cl_detail['per_ti_score'];
        }elseif($cl_detail['score_type'] == 2){
            if($answer_arr['right_num'] == ($answer_arr['right_num'] + $answer_arr['wrong_num'])){
                $score = $cl_detail['per_ti_score'];
            }
        }

        //检查今天是否第一次答题
        $zeng_score = 0;
        $r = $this->check_first_ci($clc_detail);
        if($r){
            //计算连续天数
            $day = $this->count_lianxu_day($clc_detail);
            $zeng_score = $day * $cl_detail['zeng_score'];
        }

        $clc_data = [];
        $clc_data['right_num'] = $answer_arr['right_num'];
        $clc_data['wrong_num'] = $answer_arr['wrong_num'];
        $clc_data['score'] = $score;
        $clc_data['zeng_score'] = $zeng_score;

        $maps = [];
        $maps[] = ['id','=',$clc_detail['id']];
        $r = model('class_learn_ci')->where($maps)->update($clc_data);
        if($r !== false){
            //更新小课堂学习积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_cl_integral($clc_detail['id']);

            return 1;
        }
        return 0;
    }

    /**
     * 处理答案，返回对与错的数量
     * @param $clc_detail
     * @return mixed
     */
    public function deal_answer($clc_detail){
        $maps = [];
        $maps[] = ['openid','=',$clc_detail['openid']];
        $maps[] = ['clid','=',$clc_detail['clid']];
        $maps[] = ['clcid','=',$clc_detail['id']];
        $list = model('class_learn_record')->get_list($maps);
        $right_num = $wrong_num = 0;
        foreach ($list as $k => $v){
            $maps = [];
            $maps[] = ['id','=',$v['ctid']];
            $ct_detail = model('class_ti')->get_detail($maps,'answer');

            //检查答案是否正确
            if($this->check_answer($v['answer'],$ct_detail['answer'])){
                $right_num++;
            }else{
                $wrong_num++;
            }
        }
        $data['right_num'] = $right_num;
        $data['wrong_num'] = $wrong_num;
        return $data;
    }

    /**
     * 检查答案是否正确
     * @param $user_anwser
     * @param $ti_anwser
     * @return int
     */
    public function check_answer($user_anwser,$ti_anwser){
        $user_anwser_arr = str_split($user_anwser,1);
        $ti_anwser_arr = str_split($ti_anwser,1);

        sort($user_anwser_arr);
        sort($ti_anwser_arr);
        if($user_anwser_arr === $ti_anwser_arr){
            return 1;
        }
        return 0;
    }

    /**
     * 检查今天是否第一次答题
     * @param $clc_detail
     * @return int
     */
    public function check_first_ci($clc_detail){
        $start_time = date('Y-m-d 00:00:00',strtotime($clc_detail['start_time']));

        $maps = [];
        $maps[] = ['openid','=',$clc_detail['openid']];
        $maps[] = ['clid','=',$clc_detail['clid']];
        $maps[] = ['start_time','>=',$start_time];
        $detail = model('class_learn_ci')->get_detail($maps,'id','id asc');
        if($clc_detail['id'] == $detail['id']){
            return 1;
        }
        return 0;
    }

    /**
     * 计算连续天数
     * @param $clc_detail
     * @return int
     */
    public function count_lianxu_day($clc_detail){
        $maps = [];
        $maps[] = ['openid','=',$clc_detail['openid']];
        $maps[] = ['clid','=',$clc_detail['clid']];
        $maps[] = ['start_time','<',$clc_detail['start_time']];
        $list = model('class_learn_ci')->get_list($maps,'start_time','id desc');

        $ci_time_arr = [];
        foreach ($list as $k => $v){
            $ci_time_arr[] = date('Y-m-d',strtotime($v['start_time']));
        }
        $ci_time_arr = array_values(array_unique($ci_time_arr));
        $start_date = date('Y-m-d',strtotime($clc_detail['start_time']));

        //计算答题天数
        $day = $this->count_dati_day($start_date,$ci_time_arr);
        return $day;
    }

    /**
     * 计算答题天数
     * @param $start_time
     * @param $dates
     * @param int $i
     * @return int
     */
    public function count_dati_day($start_time,$dates,$i=1){
        $first_time = date('Y-m',strtotime($start_time)) . '-01';
        if($first_time === $start_time){
            return $i;
        }
        $start_time = date('Y-m-d',strtotime($start_time . '-1 day'));
        if(in_array($start_time,$dates)){
            $i++;
            $i = $this->count_dati_day($start_time,$dates,$i);
        }
        return $i;
    }

    /**
     * 检查题型
     * 1：单选题；
     * 2：多选题；
     * 3：判断题；
     * @param $ct_detail
     * @return int
     */
    public function check_ti_type($ct_detail){
        if(strlen($ct_detail['answer']) > 1){
            return 2;
        }
        $option = json_decode($ct_detail['option'],true);
        if(count($option) == 2){
            return 3;
        }
        return 1;
    }
}